package com.serviceprovider.business.module.admin.license.model;

//~--- non-JDK imports --------------------------------------------------------

import org.springframework.web.multipart.MultipartFile;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;

public class LicenseForm {
    private boolean       deleteFile;
    private MultipartFile file;
    private License       license;

    public LicenseForm() {
        this.license = new License();
        this.license.setActive(true);
    }

    public LicenseForm(License license) {
        this.license = license;
    }

    public License getLicense() {
        return license;
    }

    public void setLicense(License license) {
        this.license = license;
    }

    public MultipartFile getFile() {
        return file;
    }

    public void setFile(MultipartFile file) {
        this.file = file;
    }

    public boolean isDeleteFile() {
        return deleteFile;
    }

    public void setDeleteFile(boolean deleteFile) {
        this.deleteFile = deleteFile;
    }

    public byte[] getBytes() throws IOException {
        return (file == null)
               ? null
               : file.getBytes();
    }

    public void applyUploadedData() throws IOException {
        if (deleteFile) {
            license.setAttachments(null);
            license.setFilename(null);
        }

        byte[] fileData = (file == null)
                          ? null
                          : file.getBytes();

        if (fileData != null) {
            license.setAttachments(fileData);
            license.setFilename(file.getOriginalFilename());
        }
    }
}



